﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using TrafficArchives.Common;
using TrafficArchives.DataAccess;

namespace TrafficArchives.Users
{
    public partial class RoleView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                QueryData();
            }
        }

        private void QueryData()
        {
            var request = new Request { Pagination = { Enabled = true, PageSize = MainPager.PageSize, PageIndex = MainPager.CurrentPageIndex - 1 } };
           
            var db = new RoleDb();
            var data = db.GetData(request);
            MainPager.RecordCount = data.RecordCount;
            GridViewMain.DataSource = data.Data;
            GridViewMain.DataBind();

            // 没数据时，显示表头。
            TableHeader.Visible = data.RecordCount == 0;
            UpdatePanelMain.Update();
        }

        /// <summary>
        /// 数据处按钮处理。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridViewMainRowCommand(object sender, GridViewCommandEventArgs e)
        {
            var id = e.CommandArgument.ToString();
            var db = new RoleDb();
            switch (e.CommandName)
            {
                case "Edit":
                    Response.Redirect(string.Format("RoleEdit.aspx?action=edit&id={0}", id));
                    break;
                case "Del":
                    db.Delete(new List<string> { id });
                    QueryData();
                    break;
            }
        }

        protected void MainPagerPageIndexChanged(object sender, EventArgs e)
        {
            QueryData();
        }

        protected void BtnQueryClicked(object sender, EventArgs e)
        {
            MainPager.CurrentPageIndex = 1;

            QueryData();
        }
    }
}